package com.bosssoft.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bosssoft.entity.dto.FibSealDTO;
import com.bosssoft.entity.po.FibVoucherPO;
import com.bosssoft.entity.vo.FibSealVO;
import com.bosssoft.enums.EleSerCode;
import com.bosssoft.exception.DaoException;
import com.bosssoft.util.SnowflakeIdUtil;
import com.sun.javafx.util.Utils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bosssoft.mapper.FibSealMapper;
import com.bosssoft.entity.po.FibSealPO;
import com.bosssoft.service.FibSealService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import static com.alibaba.csp.sentinel.cluster.log.ClusterClientStatLogUtil.log;

/**
 * @author Li kexi
 * Created on 2021/8/16.14:36
 */
@Service
public class FibSealServiceImpl extends ServiceImpl<FibSealMapper, FibSealPO> implements FibSealService {

    /**
     * 根据id删除
     *
     * @param fibSealDTO
     */
    @Override
    public void updateDeleted(FibSealDTO fibSealDTO) {
        FibSealPO fibSealPO = new FibSealPO();
        BeanUtils.copyProperties(fibSealDTO, fibSealPO);
        //id判空
        if (StringUtils.isEmpty(fibSealPO.getSealId())) {
            log("更新id为空,in seal_updateDeleted方法");
            throw DaoException.error(EleSerCode.SEAL_ID_NOT_FOUND);
        }
        //创建人判空
        if (StringUtils.isEmpty(fibSealPO.getUpdateBy())) {
            throw DaoException.error(EleSerCode.UPDATEBY_NAME_NOT_FOUND);
        }
        QueryWrapper<FibSealPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("seal_id", fibSealPO.getSealId());
        FibSealPO sqlPO = baseMapper.selectOne(queryWrapper);
        //数据库记录判空
        if (sqlPO == null) {
            log("前台传id值为" + fibSealPO.getSealId() + "根据code查找,数据库没有记录,in seal_updateDeleted方法");
            throw DaoException.error(EleSerCode.VOUCHER_RECODE_NOT_FOUND);
        }
        //该条记录已禁用
        if (StringUtils.isEmpty(sqlPO.getDeleted())) {
            log("id值为" + fibSealPO.getSealId() + "的记录已被禁用,in seal_updateDeleted方法");
            throw DaoException.error(EleSerCode.VOUCHER__RECODE_IS_DELETED);
        }
        fibSealPO.setDeleted((byte) -1);
        baseMapper.updateById(fibSealPO);
    }

    @Override
    public int updateBatch(List<FibSealPO> list) {
        return baseMapper.updateBatch(list);
    }

    @Override
    public int updateBatchSelective(List<FibSealPO> list) {
        return baseMapper.updateBatchSelective(list);
    }

    @Override
    public int batchInsert(List<FibSealPO> list) {
        return baseMapper.batchInsert(list);
    }

    @Override
    public int insertOrUpdate(FibSealDTO record) {
        FibSealPO fibSealPO = new FibSealPO();
        BeanUtils.copyProperties(record, fibSealPO);
        //判断id是否为空
        if (StringUtils.isEmpty(record.getSealId())) {
            //不查重:名字可重复,id自增,number雪花,都不会重复
            //如果id为空,说明是新增,需要补全初始化信息
            //如果创建人为空,返回错误数据
            if (StringUtils.isEmpty(fibSealPO.getCreateBy())) {
                throw DaoException.error(EleSerCode.CREATEBY_NAME_NOT_FOUND);
            }
            //雪花算法生成编号
            fibSealPO.setSealNumber(SnowflakeIdUtil.getSnowflakeId());
            //版本号
            fibSealPO.setVersion(1);
            //删除值
            fibSealPO.setDeleted((byte) 1);
        } else {
            //如果id不为空,说明是修改
            FibSealPO sqlSeal = baseMapper.selectById(record.getSealId());
            //查空
            if (sqlSeal == null) {
                log.error("印章招不到");
                throw DaoException.error(EleSerCode.SEAL_NOT_FOUND);
            }
            //如果修改人为空,返回错误数据s
            if (StringUtils.isEmpty(fibSealPO.getUpdateBy())) {
                throw DaoException.error(EleSerCode.UPDATEBY_NAME_NOT_FOUND);
            }
            fibSealPO.setVersion(sqlSeal.getVersion() + 1);
            fibSealPO.setDeleted(sqlSeal.getDeleted());
        }
        return baseMapper.insertOrUpdate(fibSealPO);
    }

    @Override
    @Transactional(rollbackFor = DaoException.class)
    public void sealDeleteList(List<FibSealVO> fibSealVOList) {
        for (FibSealVO fibSealVO : fibSealVOList) {
            if (StringUtils.isEmpty(fibSealVO.getUpdateBy())) {
                throw DaoException.error(EleSerCode.UPDATEBY_NAME_NOT_FOUND);
            }
            baseMapper.deleteById(fibSealVO.getSealId());
        }
    }
}
